import logging
from google.appengine.api import users
from google.appengine.ext import db


STATE_CHOICES = (
    ('AL', 'Alabama'), ('AK', 'Alaska'), ('AZ', 'Arizona'), ('AR', 'Arkansas'),
    ('CA', 'California'), ('CO', 'Colorado'), ('CT', 'Connecticut'),
    ('DE', 'Delaware'), ('DC', 'District Of Columbia'), ('FL', 'Florida'),
    ('GA', 'Georgia'), ('HI', 'Hawaii'), ('ID', 'Idaho'), ('IL', 'Illinois'),
    ('IN', 'Indiana'), ('IA', 'Iowa'), ('KS', 'Kansas'), ('KY', 'Kentucky'),
    ('LA', 'Louisiana'), ('ME', 'Maine'), ('MD', 'Maryland'),
    ('MA', 'Massachusetts'), ('MI', 'Michigan'), ('MN', 'Minnesota'),
    ('MS', 'Mississippi'), ('MO', 'Missouri'), ('MT', 'Montana'),
    ('NE', 'Nebraska'), ('NV', 'Nevada'), ('NH', 'New Hampshire'),
    ('NJ', 'New Jersey'), ('NM', 'New Mexico'), ('NY', 'New York'),
    ('NC', 'North Carolina'), ('ND', 'North Dakota'), ('OH', 'Ohio'),
    ('OK', 'Oklahoma'), ('OR', 'Oregon'), ('PA', 'Pennsylvania'),
    ('RI', 'Rhode Island'), ('SC', 'South Carolina'), ('SD', 'South Dakota'),
    ('TN', 'Tennessee'), ('TX', 'Texas'), ('UT', 'Utah'), ('VT', 'Vermont'),
    ('VA', 'Virginia'), ('WA', 'Washington'), ('WV', 'West Virginia'),
    ('WI', 'Wisconsin'), ('WY', 'Wyoming'), ('AB', 'Alberta'),
    ('BC', 'British Columbia'), ('MB', 'Manitoba'), ('NB', 'New Brunswick'),
    ('NL', 'Newfoundland and Labrador'), ('NT', 'Northwest Territories'),
    ('NS', 'Nova Scotia'), ('NU', 'Nunavut'), ('ON', 'Ontario'),
    ('PE', 'Prince Edward Island'), ('QC', 'Quebec'), ('SK', 'Saskatchewan'),
    ('YT', 'Yukon'))


COUNTRY_CHOICES = (
    ('US', 'USA'), ('CA', 'Canada'))


class User(db.Model):
    guser       = db.UserProperty(required=True)
    gmail       = db.EmailProperty(required=True)
    gnick       = db.StringProperty(required=True)
    nick        = db.StringProperty(required=True)
    ctime       = db.DateTimeProperty(auto_now_add=True)
    lastseen    = db.DateTimeProperty()

    @classmethod
    def get_user_for_guser(cls, guser):
        """Get a User for a Google Account User. Create a new User if this is
        the first time this Google Account User logs in.
        """

        # User '<gmail_address>' as the key.
        gmail = guser.email()
        assert gmail
        key = '<%s>' % gmail

        # Prepare the new User dictionary if this is the first time.
        udict = {}
        udict['guser'] = guser
        udict['gmail'] = gmail
        udict['gnick'] = guser.nickname()
        udict['nick']  = udict['gnick']

        return cls.get_or_insert(key, **udict)


class MapPlace(db.Model):
    cname       = db.StringProperty()
    ename       = db.StringProperty()
    street      = db.StringProperty(required=True)
    suite       = db.StringProperty()
    city        = db.StringProperty(required=True)
    state       = db.StringProperty(required=True)
    zip         = db.StringProperty(required=True)
    country     = db.StringProperty(required=True)
    lat         = db.FloatProperty(required=True)
    lng         = db.FloatProperty(required=True)
    phone       = db.PhoneNumberProperty(required=False)
    fax         = db.PhoneNumberProperty()
    web         = db.LinkProperty()
    createdby   = db.ReferenceProperty(User, collection_name='user_x_set')
    modifiedby  = db.ReferenceProperty(User, collection_name='user_y_set')
    ctime       = db.DateTimeProperty(auto_now_add=True)
    mtime       = db.DateTimeProperty(auto_now=True)
